/*
 * Copyright (c) 2023. Shanghai HEADING information Engineering Co., Ltd. All rights reserved.
 */

package com.lovely602.boot.bootdemo.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

/**
 * @author lizhixing
 */
public class ThreadPoolHelper {

  private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 50, 2,
      TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000),
      new ThreadFactoryBuilder().setNameFormat("demo-threadPool-%d").build());

  /**
   * 正确的使用
   */
  public static ThreadPoolExecutor getRightThreadPool() {
    return threadPoolExecutor;
  }

  /**
   * 错误的使用
   */
  public static ThreadPoolExecutor getWrongThreadPool() {
    // 线程池没有复用
    return (ThreadPoolExecutor) Executors.newCachedThreadPool();
  }

}
